<?php

/**
 * This is the model class for table "{{permission}}".
 *
 * The followings are the available columns in table '{{permission}}':
 * @property string $id
 * @property integer $group_id
 * @property integer $role_id
 * @property string $view_access
 * @property string $update_access
 * @property string $created
 * @property string $modified
 */
class Permission extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Permission the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{permission}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('group_id, role_id, created, modified', 'required'),
			array('group_id, role_id', 'numerical', 'integerOnly'=>true),
			array('view_access, update_access', 'length', 'max'=>3),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, group_id, role_id, view_pr, update_pr, created, modified', 'safe', 'on'=>'search'),
			array('modified','default',
           	  'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'update'),
        	array('created,modified','default',
              'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'insert'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'group_id' => 'Group',
			'role_id' => 'Role',
			'view_access' => 'View Access',
			'update_access' => 'Update Access',
			'created' => 'Created',
			'modified' => 'Modified',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('group_id',$this->group_id);
		$criteria->compare('role_id',$this->role_id);
		$criteria->compare('view_access',$this->view_access,true);
		$criteria->compare('update_access',$this->update_access,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public static function getList($type, $arrParams=null)
		{
			switch ($type)
			{
				case 'GROUP-ID':
						$arrList = Permission::model()->findAllByAttributes(array('role_id'=>(int)$arrParams['role_id']));
						foreach ($arrList as $List)
						{
							$AllList[$List->id] = $List->name;
						}
						//echo '<pre>';print_r($AllList);exit;
						$arrData = $AllList;
						return $arrData;
					break;
			}
		}
}